package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.InventoryDto;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.SaleCarDto;
import com.zw.stock_sell_storage.dto.ex.InventoryDtoEx;
import com.zw.stock_sell_storage.mapper.InventoryMapper;
import com.zw.stock_sell_storage.service.IInventoryService;
import com.zw.stock_sell_storage.service.ISaleCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, InventoryDto> implements IInventoryService {
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    ISaleCarService iSaleCarService;
    @Override
    public InventoryDto queryInventoryByGoodsCode(String goodsCode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_code",goodsCode);
        return inventoryMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public boolean addInventory(InventoryDto inventoryDto) {
        inventoryMapper.insert(inventoryDto);
        return true;
    }

    @Override
    @Transactional
    public boolean updateInventory(InventoryDto inventoryDto) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("goods_id",inventoryDto.getGoodsId());
        inventoryMapper.update(inventoryDto,updateWrapper);
        //同步更新销售购物车商品销售价格
        iSaleCarService.updateSaleCarByGoodsCode(inventoryDto.getGoodsCode(),inventoryDto.getGoodsSalePrice());
        return true;
    }

    @Override
    public Page<InventoryDto> queryInventoryList(MyPageDto<InventoryDtoEx> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getGoodsName()!=null&&!"".equals(myPageDto.getData().getGoodsName()))
        queryWrapper.like("goods_name",myPageDto.getData().getGoodsName());
        if (myPageDto.getData().getGoodsType()!=null&&!"".equals(myPageDto.getData().getGoodsType())){
            queryWrapper.eq("goods_type",myPageDto.getData().getGoodsType());
        }
        if (myPageDto.getData().getStartNumber()!=null&&myPageDto.getData().getStartNumber()>0){
            queryWrapper.ge("goods_number",myPageDto.getData().getStartNumber());
        }
        if (myPageDto.getData().getEndNumber()!=null&&myPageDto.getData().getEndNumber()>0){
            queryWrapper.le("goods_number",myPageDto.getData().getEndNumber());
        }
        if (myPageDto.getData().getGoodsSalePriceFalse()!=null&&myPageDto.getData().getGoodsSalePriceFalse()){
            queryWrapper.le("goods_sale_price",0);
        }
        return inventoryMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    public InventoryDto queryInventoryById(int goodsId) {

        return inventoryMapper.selectById(goodsId);
    }
}
